<?php

namespace app\common\command;

use app\common\model\card\Card;
use payment\{SlashService, WorldFirstService, AirwallexService};
use think\console\Command;
use think\console\input\Argument;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class QueryCardDetail extends Command
{
    protected function configure()
    {
        $this->setName('query_card_detail')
            ->addArgument('type', Argument::OPTIONAL, "worldfirst or slash")
            ->setDescription('该功能用于同步卡片详情');
    }

    protected function execute(Input $input, Output $output)
    {
        $type = trim($input->getArgument('type'));
        if (!in_array($type, ['worldfirst', 'worldfirst_hk', 'slash'])) {
            $output->error('type 参数必须是 "worldfirst" 或 "slash"');
            return;
        }
        $output->writeln("开始更新卡片详情");
        if ($type == 'slash') {
            $this->slash(12);
            $this->slash(15);
        }

        if ($type == 'worldfirst') {
            $worldFirst = new WorldFirstService('worldfirst');
            $this->worldfirst($worldFirst);
        }
        if ($type == 'worldfirst_hk') {
            $worldFirst = new WorldFirstService('worldfirst_hk');
            $this->worldfirst($worldFirst);
        }
    }

    private function worldfirst($worldFirst)
    {
        $cards = Card::where('card_bin', 10)->select()->toArray();
        $begin = time();
        dump("总数:" . count($cards));
        $logFile = '/dev/shm/worldfirst_limit_' . date('YmdHi');
        foreach ($cards as $card) {
            $result = $worldFirst->getCardDetail($card['card_id']);
            file_put_contents($logFile, json_encode($result) . "\n", FILE_APPEND);

            if (isset($result['cardLimitDetail']['monthlyLimit'])) {
                $perCardLimit = $result['cardLimitDetail']['perCardLimit'] ?? $result['cardLimitDetail']['monthlyLimit'];
                $row = [
                    'spend_money' => $perCardLimit['used']['value'] / 100,
                    'limit_money' => $perCardLimit['range']['value'] / 100
                ];
                Card::where('card_id', $card['card_id'])->update($row);
            }

        }
        $end = time();
        dump('time:' . $end - $begin);

    }


    private function slash($type = 12)
    {
        $slashService = new SlashService($type);
        $cards = Card::where('card_bin', $type)->where('status', 1)->select()->toArray();
        $begin = time();
        dump("总数:" . count($cards));
        foreach ($cards as $card) {
            $result = $slashService->getCardUtilization($card['card_id']);
            dump($result);
            if (isset($result['spend']['amountCents'])) {
                $available = $result['availableBalance']['amountCents'] ?? 0;
                $row = [
                    'spend_money' => $result['spend']['amountCents'] / 100,
                ];
                if ($available) $row['limit_money'] = ($available + $result['spend']['amountCents']) / 100;
                Card::where('card_id', $card['card_id'])->update($row);
            }
        }
        $end = time();
        dump('time:' . $end - $begin);

    }
}